﻿using System.Collections.Generic;
using ExcelLibrary.SpreadSheet;
using HappyBa.Core.Data;
using HappyBa.Core.Interfaces;
using HappyBa.Localization;

namespace HappyBa.Core.Reporting
{
    class FullReportGenerator : IFullReportGenerator
    {
        private readonly IReportFilePathGetter reportFilePathGetter;
        private readonly IDataCache dataCache;

        public FullReportGenerator(IReportFilePathGetter reportFilePathGetter, IDataCache dataCache)
        {
            this.reportFilePathGetter = reportFilePathGetter;
            this.dataCache = dataCache;
        }

        public void GenerateFullReport()
        {
            reportFilePathGetter.Build(x=> Create(x, dataCache.Metrics.EstimatableEntities));
        }

        private static void Create(string fileName, IList<UIEstimatableEntityBase> entities )
        {
            var workbook = new Workbook();
            var worksheet = new Worksheet(HappyBaLang.ExcelReport);
            worksheet.Cells[0, 0] = new Cell(HappyBaLang.Id);
            worksheet.Cells[0, 1] = new Cell(HappyBaLang.Type);
            worksheet.Cells[0, 2] = new Cell(HappyBaLang.Name);

            var lineNo = 1;
            foreach (var e in entities)
            {
                worksheet.Cells[lineNo, 0] = worksheet.BuildCell(e.Id);
                worksheet.Cells[lineNo, 1] = worksheet.BuildCell(e.EntityType);
                worksheet.Cells[lineNo, 2] = worksheet.BuildCell(e.Name);
                ++lineNo;
            }
            workbook.SaveResults(fileName, lineNo, worksheet);
        }

    }
}